﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ATEC.ATDataCoreNET;
using ATEC.GeometryNET;
using SSDPNET;
using SSDPNET.SZEWObject;
using SSDPNET.DataCore;
using SSDPNET.Geom;
using ATEC.ATDataCoreNET.ATObjects;
using ATEC.ATDataCoreNET.ATCCL;

namespace SZEWTerrainUtilExample
{
    public partial class TerrainTestForm : Form
    {
        private void AddMeshToScene(PolyfaceMeshObject meshIn, int color)
        {
            //获取当前激活Scene
            ATSceneRef sceneActive = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ATSceneRef scene = SZEWTerrainUtil.GetTerrainScene(sceneActive.GetATProject());
       
            ATEC.ATDataCoreNET.ATObjects.ATObject obj =
                ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(scene.GetATScene(), meshIn, null);
            ObjectPropertiesSetter setter = new ObjectPropertiesSetter();
            setter.SetColor((uint)color);
            setter.Apply(obj);
            obj.AddToScene();
        }

        private void AddCrveToScene(CurveSet crvIn, int color)
        {
            //获取当前激活Scene
            ATSceneRef sceneActive = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ATSceneRef scene = SZEWTerrainUtil.GetTerrainScene(sceneActive.GetATProject());
        
            ATEC.ATDataCoreNET.ATObjects.ATObject obj =
                ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(scene.GetATScene(), crvIn, null);
            ObjectPropertiesSetter setter = new ObjectPropertiesSetter();
            setter.SetColor((uint)color);
            setter.Apply(obj);
            obj.AddToScene();
        }
        private void AddPointToScene(XPoint3d origionPt, double rd, int color)
        {
            ATSphereDetail detail2 = new ATSphereDetail(origionPt, rd);
            BaseSolid sphere = BaseSolid.CreateATSphere(detail2);
            PolyfaceMeshObject sphereMesh = IPolyfaceConstructionExtention.CreateFromBaseSolid(sphere);
            AddMeshToScene(sphereMesh, color);
        }


        public TerrainTestForm()
        {
            InitializeComponent();
        }

        private void button_GetAllTerrainInfo_Click(object sender, EventArgs e)
        {
            List<TerrainInfo> infos = new List<TerrainInfo>();

            if (!SZEWTerrainUtil.GetAllTerrainInfo(out infos))
                return;
            string messageStr = "";
            for(int i=0;i<infos.Count;i++)
            {
                string singleMessageStr = string.Format("边界体id: {0} ; 面片id: {1} \n ", infos[i].BoundaryId,
                 infos[i].SurfaceId);
                messageStr += singleMessageStr;
            }
            MessageBox.Show(messageStr);
        }


        private bool GetFirstTerrainInfo(ref TerrainInfo info)
        {
            List<TerrainInfo> infos = new List<TerrainInfo>();

            if (!SZEWTerrainUtil.GetAllTerrainInfo(out infos))
                return false; 

            if (infos.Count == 0)
                return false;
            info = infos[0];
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TerrainInfo selectInfo = new TerrainInfo();
            if (!GetFirstTerrainInfo(ref selectInfo))
                return;

            PolyfaceMeshObject mesh = new PolyfaceMeshObject();
            if (!SZEWTerrainUtil.GetTerrainMesh(selectInfo, out mesh))
                return;
            XTransform3d trans = XTransform3d.FromTranslation(new XPoint3d(0, 0, 10000));
            mesh.Transform(ref trans, false);
            AddMeshToScene(mesh, 3);

            MessageBox.Show("生成成功！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TerrainInfo selectInfo = new TerrainInfo();
            if (!GetFirstTerrainInfo(ref selectInfo))
                return;

            XRange3d range = selectInfo.Range;
            XPoint3d centerPt = range.Low + XVector3d.UnitX * range.XLength() * 0.5 +
            XVector3d.UnitY * range.YLength() * 0.5 + XVector3d.UnitZ * range.ZLength() * 2;

            XPoint3d projectPt;
            if (!SZEWTerrainUtil.DrapePointOnTerrain(selectInfo, out projectPt, centerPt, XVector3d.UnitZ, true))
                return;
            XPoint3d testEndPt = centerPt + (-1)*XVector3d.UnitZ * range.ZLength()*5;
            CurveSet crvPj = CurveSet.CreateLinear(new List<XPoint3d>() { centerPt, testEndPt },
                CurveSet.BoundaryType.Open, false);
            AddCrveToScene(crvPj, 2);

            AddPointToScene(projectPt, range.ZLength()*0.01, 3);
            MessageBox.Show("生成成功");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            TerrainInfo selectInfo = new TerrainInfo();
            if (!GetFirstTerrainInfo(ref selectInfo))
                return;

            XRange3d range = selectInfo.Range;
            XPoint3d centerPt = range.Low + XVector3d.UnitX * range.XLength() * 0.5 +
            XVector3d.UnitY * range.YLength() * 0.5;
            centerPt.Z = 0;

            double scaled = 0.05;
            XPoint3d rectPt1 = centerPt - XVector3d.UnitX * range.XLength() * scaled -
            XVector3d.UnitY * range.YLength() * scaled;
            XPoint3d rectPt2 = centerPt + XVector3d.UnitX * range.XLength() * scaled -
         XVector3d.UnitY * range.YLength() * scaled;
            XPoint3d rectPt3 = centerPt + XVector3d.UnitX * range.XLength() * scaled +
         XVector3d.UnitY * range.YLength() * scaled;
            XPoint3d rectPt4 = centerPt - XVector3d.UnitX * range.XLength() * scaled +
         XVector3d.UnitY * range.YLength() * scaled;
            List<XPoint3d> pts = new List<XPoint3d>() { rectPt1, rectPt2, rectPt3, rectPt4 };
            CurveSet cutRegion = CurveSet.CreateLinear(pts, CurveSet.BoundaryType.Outer, false);

            PolyfaceMeshObject cutedMesh;
            if (!SZEWTerrainUtil.GetCutedMeshInTerrain(selectInfo, out cutedMesh, cutRegion))
                return;

            AddMeshToScene(cutedMesh, 3);

            MessageBox.Show("生成成功");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TerrainInfo selectInfo = new TerrainInfo();
            if (!GetFirstTerrainInfo(ref selectInfo))
                return;

            XRange3d range = selectInfo.Range;
            XPoint3d centerPt = range.Low + XVector3d.UnitX * range.XLength() * 0.5 +
            XVector3d.UnitY * range.YLength() * 0.5;
            centerPt.Z = 0;

            double scaled = 0.05;
            XPoint3d rectPt1 = centerPt - XVector3d.UnitX * range.XLength() * scaled -
            XVector3d.UnitY * range.YLength() * scaled;
            XPoint3d rectPt2 = centerPt + XVector3d.UnitX * range.XLength() * scaled -
         XVector3d.UnitY * range.YLength() * scaled;
            XPoint3d rectPt3 = centerPt + XVector3d.UnitX * range.XLength() * scaled +
         XVector3d.UnitY * range.YLength() * scaled;
            XPoint3d rectPt4 = centerPt - XVector3d.UnitX * range.XLength() * scaled +
         XVector3d.UnitY * range.YLength() * scaled;
            List<XPoint3d> pts = new List<XPoint3d>() { rectPt1, rectPt2, rectPt3, rectPt4 };
            CurveSet extractRegion = CurveSet.CreateLinear(pts, CurveSet.BoundaryType.Outer, false);

            PolyfaceMeshObject extractedMesh;
            if (!SZEWTerrainUtil.GetExtractedMeshInTerrain(selectInfo, out extractedMesh, extractRegion))
                return;

            AddMeshToScene(extractedMesh, 3);

            MessageBox.Show("生成成功");
        }
    }
}
